package com.jokerku.lottery.domain.receive.engine.impl;

import com.jokerku.lottery.domain.receive.engine.EngineBase;
import com.jokerku.lottery.domain.receive.filter.EventFilter;
import com.jokerku.lottery.domain.receive.model.BehaviorMatter;
import com.jokerku.lottery.domain.receive.model.MessageTextEntity;
import com.jokerku.lottery.infrastructure.common.EventTypeEnum;
import com.jokerku.lottery.infrastructure.utils.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: guzq
 * @CreateTime: 2022/08/31 11:27
 * @Description: 消息引擎处理器
 * @Version: 1.0
 */
@Service("msgEngineHandle")
public class MsgEngineHandle extends EngineBase {

    @Value("${wx.config.originalid}")
    private String originalId;

    @Override
    public String process(BehaviorMatter req) {
        EventFilter router = super.router(req);
        if (Objects.isNull(router)) {
            return null;
        }

        String handleResult = router.filter(req);
        if (StringUtils.isBlank(handleResult)) {
            return "";
        }


        //反馈信息[文本]
        MessageTextEntity res = new MessageTextEntity();
        res.setToUserName(req.getOpenId());
        res.setFromUserName(originalId);
        res.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000L));
        res.setMsgType(EventTypeEnum.TEXT.name());
        res.setContent(handleResult);
        return XmlUtil.beanToXml(res);
    }
}
